/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import ManejoExc.EmpresaException;
import Negocio.Empresa;
import Negocio.Plantacion;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Diego
 */
public class FrmPlantacion extends javax.swing.JDialog {
     private DefaultListModel modelPlantaciones = new DefaultListModel();
     private Empresa unaEmpresa;
     private Plantacion unaPlantacion;
    /**
     * Creates new form FrmPlantacion
     */
    public FrmPlantacion(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public FrmPlantacion(java.awt.Frame parent, boolean modal,Empresa unaEmpresa) {
        super(parent, modal);
        initComponents();
        this.unaEmpresa = unaEmpresa;
        this.LstPlantaciones.setModel(modelPlantaciones);
        this.inicializar();
    }
    
    public void limpiarCampos(){
        this.unaPlantacion = null;
        this.TxtNombre.setText("");
        this.TxtPropietario.setText("");
        this.TxtUbicacion.setText("");
        this.BtnNuevo.setEnabled(true);
        this.BtnGuardar.setEnabled(false);
        this.BtnBorrar.setEnabled(false);
        this.BtnCancelar.setEnabled(false);
        this.TxtNombre.setEnabled(false);
        this.TxtPropietario.setEnabled(false);
        this.TxtUbicacion.setEnabled(false);
        
    }
    
    public void inicializar(){
        this.limpiarCampos();
        this.cargarPlantaciones();
        this.unaPlantacion = null;
        this.BtnCancelar.setEnabled(false);
        this.BtnNuevo.setEnabled(true);
        this.BtnGuardar.setEnabled(false);
        this.unaPlantacion = null;
    }
    
    public void cargarPlantaciones(){
        this.modelPlantaciones.clear();
        if(this.unaEmpresa.getPlantacionesActivas().size()> 0){
            Iterator it = this.unaEmpresa.getPlantacionesActivas().iterator();
            while(it.hasNext()){
                this.modelPlantaciones.addElement((Plantacion) it.next());
            }
        }else{
             this.modelPlantaciones.addElement("No se encontraron Plantaciones activas");
        }
    }
    
    public void cargarCampos(){
        this.TxtNombre.setText(this.unaPlantacion.getNombre());
        this.TxtPropietario.setText(this.unaPlantacion.getPropietario());
        this.TxtUbicacion.setText(this.unaPlantacion.getUbicacion());
        
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        LstPlantaciones = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        TxtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        TxtPropietario = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        TxtUbicacion = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        BtnNuevo = new javax.swing.JButton();
        BtnGuardar = new javax.swing.JButton();
        BtnCancelar = new javax.swing.JButton();
        BtnAceptar = new javax.swing.JButton();
        BtnBorrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        LstPlantaciones.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        LstPlantaciones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LstPlantacionesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(LstPlantaciones);

        jLabel1.setText("Nombre");

        TxtNombre.setEnabled(false);

        jLabel2.setText("Propietario");

        TxtPropietario.setEnabled(false);

        jLabel3.setText("Ubicacion");

        TxtUbicacion.setEnabled(false);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Plantaciones");

        BtnNuevo.setText("Nuevo");
        BtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnNuevoActionPerformed(evt);
            }
        });

        BtnGuardar.setText("Guardar");
        BtnGuardar.setEnabled(false);
        BtnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnGuardarActionPerformed(evt);
            }
        });

        BtnCancelar.setText("Cancelar");
        BtnCancelar.setEnabled(false);
        BtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCancelarActionPerformed(evt);
            }
        });

        BtnAceptar.setText("Aceptar");
        BtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAceptarActionPerformed(evt);
            }
        });

        BtnBorrar.setText("Borrar");
        BtnBorrar.setEnabled(false);
        BtnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnBorrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(BtnAceptar)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(BtnNuevo)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(BtnGuardar)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(BtnBorrar)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(BtnCancelar))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel3))
                                    .addGap(17, 17, 17)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(TxtUbicacion, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                        .addComponent(TxtNombre)
                                        .addComponent(TxtPropietario)))))))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(TxtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(TxtPropietario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TxtUbicacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(BtnNuevo)
                            .addComponent(BtnGuardar)
                            .addComponent(BtnCancelar)
                            .addComponent(BtnBorrar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(BtnAceptar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 453, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAceptarActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BtnAceptarActionPerformed

    private void BtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnNuevoActionPerformed
        // TODO add your handling code here:
        this.BtnCancelar.setEnabled(true);
        this.BtnNuevo.setEnabled(false);
        this.BtnGuardar.setEnabled(true);
        this.BtnBorrar.setEnabled(true);
        this.unaPlantacion = null;        
        this.TxtNombre.setText("");
        this.TxtPropietario.setText("");
        this.TxtUbicacion.setText("");
        this.TxtNombre.setEnabled(true);
        this.TxtPropietario.setEnabled(true);
        this.TxtUbicacion.setEnabled(true);
    }//GEN-LAST:event_BtnNuevoActionPerformed

    private void BtnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnGuardarActionPerformed
        // TODO add your handling code here:
        try{
        if(this.unaPlantacion == null){
            this.unaEmpresa.nuevaPlantacion(this.TxtNombre.getText(),this.TxtPropietario.getText(), this.TxtUbicacion.getText());
        }else{
            this.unaPlantacion.actualizar(this.TxtNombre.getText(),this.TxtPropietario.getText(), this.TxtUbicacion.getText());
        }
        this.inicializar();
        }
        catch(EmpresaException ex){           
            JOptionPane.showMessageDialog(this, ex.getMessage()); 
        }
    }//GEN-LAST:event_BtnGuardarActionPerformed

    private void LstPlantacionesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LstPlantacionesMouseClicked
        // TODO add your handling code here:
         this.unaPlantacion = (Plantacion )this.modelPlantaciones.getElementAt(this.LstPlantaciones.getSelectedIndex());
         this.BtnNuevo.setEnabled(false);
         this.BtnGuardar.setEnabled(true);
         this.BtnCancelar.setEnabled(true);
         this.BtnBorrar.setEnabled(true);
         this.TxtNombre.setEnabled(true);
         this.TxtPropietario.setEnabled(true);
         this.TxtUbicacion.setEnabled(true);
         this.cargarCampos();
    }//GEN-LAST:event_LstPlantacionesMouseClicked

    private void BtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelarActionPerformed
        // TODO add your handling code here:
        this.inicializar();
       
    }//GEN-LAST:event_BtnCancelarActionPerformed

    private void BtnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnBorrarActionPerformed
        // TODO add your handling code here:
        int mc,bc,ch=1;
        mc = JOptionPane.QUESTION_MESSAGE;
        bc = JOptionPane.YES_NO_CANCEL_OPTION;
        ch = JOptionPane.showConfirmDialog (this, "Desea Borrar la plantacion", "Confirmacion", bc, mc);
        if(ch == 0 && this.unaPlantacion != null){  
            this.unaPlantacion.deshabilitar();
            this.limpiarCampos();
            this.cargarPlantaciones();
            
        }
    }//GEN-LAST:event_BtnBorrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void mains(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmPlantacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmPlantacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmPlantacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmPlantacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmPlantacion dialog = new FrmPlantacion(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAceptar;
    private javax.swing.JButton BtnBorrar;
    private javax.swing.JButton BtnCancelar;
    private javax.swing.JButton BtnGuardar;
    private javax.swing.JButton BtnNuevo;
    private javax.swing.JList LstPlantaciones;
    private javax.swing.JTextField TxtNombre;
    private javax.swing.JTextField TxtPropietario;
    private javax.swing.JTextField TxtUbicacion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
